package pers.sun.oauthclient.servlet;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.json.JSONObject;
import pers.sun.oauthclient.util.OAuthClientUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/5/18.
 */
public class AuthorizeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OAuthClientUtil oAuthClientUtil = new OAuthClientUtil("testclient","clientsecret","http://localhost:8080/OAuthClient/oauth/Resource");
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("state",oAuthClientUtil.getState());
        try {
            //重定向到向国家平台，发出authorize请求
            String url = oAuthClientUtil.AuthzTest();
            resp.sendRedirect(url);
        } catch (OAuthSystemException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
